package com.verlet.serviceribbon.service;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author verlet
 * @date 2018/8/3
 */
@DefaultProperties(defaultFallback = "defaultError")
@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })
    public String hiService(String name) {
        return restTemplate.getForObject("http://service-hi/hello/word?name=" + name, String.class);
    }

    public String hiError(String name) {
        return "hi," + name + ",sorry,error!";
    }

    public String defaultError(String name) {
        return "defaultError ,sorry,error!";
    }

    public String home() {
        return restTemplate.getForObject("http://service-hi//hello/word", String.class);
    }
}
